package com.hook3.pianke.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hook3.pianke.R;
import com.hook3.pianke.activity.ReadingActivity;
import com.viewpagerindicator.TabPageIndicator;

import org.w3c.dom.Text;

import java.util.ArrayList;


/**
 * Created by Administrator on 2015/12/10.
 */
public class BaseReadDetailsActivity extends AppCompatActivity {


    public ViewPager viewpager_readactivity_details;
    public ArrayList<ReadDetalsCommon> readDetalsCommonList = new ArrayList<ReadDetalsCommon>();
    public int position;
    public ReadingActivity.JSON json;
    public String title;
    public String type;
    public Toolbar toolbar;
    public TextView tv_readingac_addTime;
    public TextView tv_readingac_hot;
    public ArrayList<TextView> textviewList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridviewdetails);

        init();


        viewpager_readactivity_details = (ViewPager) findViewById(R.id.viewpager_readactivity_details);
        MyMorningPagerAdapter adapter = new MyMorningPagerAdapter();

        viewpager_readactivity_details.setAdapter(adapter);


        //设置pager页面监听，实现滑动改变textview最新最热
//        gridview_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });

        tv_readingac_addTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewpager_readactivity_details.setCurrentItem(0);
            }
        });

        tv_readingac_hot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewpager_readactivity_details.setCurrentItem(1);
            }
        });

    }

    private void init() {
        tv_readingac_addTime = (TextView) findViewById(R.id.tv_readingac_addTime);
        tv_readingac_hot = (TextView) findViewById(R.id.tv_readingac_hot);
        textviewList = new ArrayList<TextView>();
        textviewList.add(tv_readingac_addTime);
        textviewList.add(tv_readingac_hot);

        Intent intent = getIntent();
        position = intent.getIntExtra("position", 0);
        Bundle bundle = intent.getBundleExtra("bjson");
        json = (ReadingActivity.JSON) bundle.getSerializable("json");
        title = json.data.list.get(position).name;
        type = json.data.list.get(position).type;


        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(title);
        toolbar.setNavigationIcon(R.mipmap.abc_ic_ab_back_mtrl_am_alpha);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        readDetalsCommonList.add(new ReadDetalsCommon(this, type, "addtime"));
        readDetalsCommonList.add(new ReadDetalsCommon(this, type, "hot"));


//        ImageView imageView1 = new ImageView(this);
//        ImageView imageView2 = new ImageView(this);
//        imageView1.setBackgroundResource(R.mipmap.ic_back_black);
//        imageView1.setBackgroundResource(R.mipmap.ic_flowers);
//        imagelist = new ArrayList<ImageView>();
//        imagelist.add(imageView1);
//        imagelist.add(imageView2);
    }


    class MyMorningPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            Log.e("TAG", "readDetalsCommonList的个数" + readDetalsCommonList.size());
            return readDetalsCommonList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            Log.e("TAG", "装配viewpager");
            ReadDetalsCommon readDetalsCommon = readDetalsCommonList.get(position);
            readDetalsCommon.initView();
            readDetalsCommon.initData();
            View view = readDetalsCommon.view;
            container.addView(view);

            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

    }
}





